package net.zjitc.controller.client;

import net.zjitc.common.Msg;
import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/26 11:11
 */
@Controller

public class UserController {

    @Autowired
    UserService userService;
    @RequestMapping("/findAll")
    public String findAll(Model modle){
        List<User> list = userService.findAll();
        modle.addAttribute("list",list);
        return "/list.jsp";
    }
    @RequestMapping("/loginUI")
    public String loginUI(){
        return "/client/login.jsp";
    }

    @RequestMapping(value = "/login")
    public String login(HttpSession session,User user){
        User u =  userService.login(user.getUsername(),user.getPassword());
        if (u != null)
        {
            session.setAttribute("user",u);
            return "/index.jsp";
        }
        return "/client/login.jsp";
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "/client/index.jsp";
    }
    @RequestMapping("/register")
    public String register(User user,Model model){
//        String username = user.getUsername();
//        String password = user.getPassword();
//        String email = user.getEmail();
//        User e = userService.checkemail(email);
//        User u = userService.checkname(username);
//            user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
    return "";

    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Msg checkName(User user){
        User u = userService.checkname(user.getUsername());
        Msg msg = new Msg();
        if (u != null)
        {
            msg.setFlag(false);
        }else {
            msg.setFlag(true);
        }
        return msg;
    }

    @RequestMapping(value = "/checkEmail")
    @ResponseBody
    public Msg checkEmail(User user){
        User u = userService.checkemail(user.getEmail());
        Msg msg = new Msg();
        if (u != null)
        {
            msg.setFlag(false);
        }else {
            msg.setFlag(true);
        }
        return msg;
    }

}
